<?php

namespace app\admin\model;

use app\common\LogService;
use think\Model;

class Order extends Model
{
    public static function list()
    {
        LogService::record(
            name: '订单<查看列表>',
            status: 1,
            user_type: 2
        );
        $param = input();
        $limit = input('limit', 10);
        $list = self::order('id desc');
        $status = input('status');
        if (isset($status) && ($status == 0 || $status == 1)) {
            $list->where(['status' => $status]);
        }
        if (!empty($param['user_id'])) {
            $list->where('user_id', $param['user_id']);
        }
        if (!empty($param['good_id'])) {
            $list->where('good_id', $param['good_id']);
        }
        if (!empty($param['out_trade_no'])) {
            $list->where('out_trade_no', $param['out_trade_no']);
        }
        $list->order('id', 'desc')->order('status', 'asc');
        $list = $list->paginate($limit);
        return $list;
    }
    public function del($id)
    {
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '订单<删除>',
                // user_id: $this->uid,
                target_id: $id,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: 2

            );
        };
        $ids = explode(',', $id);
        $res = self::destroy($ids);
        if ($res) {
            $recordLog(1);
            return true;
        }
        $recordLog(0);
        return false;
    }
}
